package com.cali.libcore.view.compose

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.Gravity.CENTER_VERTICAL
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.cali.common.kt.defaultTextSize
import com.cali.libcore.R
import kotlinx.android.synthetic.main.include_horizontal_two_tv.view.*

class HorizontalTwoTextView(context: Context, val attrs: AttributeSet?, def: Int) :
    LinearLayout(context, attrs, def) {

    constructor(context: Context) : this(context, null, 0)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    init {
        orientation = HORIZONTAL
        gravity = CENTER_VERTICAL
        //加载视图
        LayoutInflater.from(context).inflate(R.layout.include_horizontal_two_tv, this)
        context.theme.obtainStyledAttributes(attrs, R.styleable.HorizontalTwoTextView, 0, 0)
            ?.apply {
                tvLeft.text = getString(R.styleable.HorizontalTwoTextView_tv_left_text)
                tvRight.text = getString(R.styleable.HorizontalTwoTextView_tv_right_text)
                val tvLeftColor = getColor(R.styleable.HorizontalTwoTextView_tv_left_text_color, 0)
                val tvRightColor =
                    getColor(R.styleable.HorizontalTwoTextView_tv_right_text_color, 0)
                val tvLeftSize = getDimensionPixelSize(
                    R.styleable.HorizontalTwoTextView_tv_left_text_size,
                    context.defaultTextSize()
                )
                val tvRightSize = getDimensionPixelSize(
                    R.styleable.HorizontalTwoTextView_tv_right_text_size,
                    context.defaultTextSize()
                )
                tvLeft.setTextSize(TypedValue.COMPLEX_UNIT_PX, tvLeftSize.toFloat())
                tvRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, tvRightSize.toFloat())
                if (tvLeftColor != 0) tvLeft.setTextColor(tvLeftColor)
                if (tvRightColor != 0) tvRight.setTextColor(tvRightColor)

                val isExpandRight =
                    getBoolean(R.styleable.HorizontalTwoTextView_is_expand_right, false)

                if (isExpandRight) {
                    tvRight.layoutParams = LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f)
                    tvRight.gravity = Gravity.RIGHT
                }
                recycle()
            }
    }

    fun setLeftText(content: String) {
        tvLeft.text = content
    }

    fun setRightText(content: String) {
        tvRight.text = content
    }
}